package com.foreknow.controller;

import com.foreknow.bean.Guestbook;
import com.foreknow.service.GuestbookService;
import com.foreknow.service.impl.GuestbookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class ManagerServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置服务器端向客户端响应的内容的类型
        resp.setContentType("text/html;charset=utf-8");
        //设置客户端向服务器端发送请求的编码格式
        req.setCharacterEncoding("utf-8");
        String method = req.getParameter("q"); //list
        if ("list".equals(method)) {
            list(req, resp);
        } else if ("delete".equals(method)) { //method=delete
            delete(req,resp);
        } else if ("jump".equals(method)) { //method=delete
            jump(req,resp);
        }else if ("add".equals(method)) { //method=delete
            add(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置服务器端向客户端响应的内容的类型
        resp.setContentType("text/html;charset=utf-8");
        //设置客户端向服务器端发送请求的编码格式
        req.setCharacterEncoding("utf-8");
        doGet(req, resp);
    }

    /**
     * 查询所有留言
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        //向网页响应内容
        PrintWriter out = resp.getWriter();
        //调用service中的方法查询所有留言的数据
        GuestbookService service = new GuestbookServiceImpl();
        List<Object> list = service.getAll();
        //如何将list集合保存到一个地方,需要将List保存到服务器端,转发之后就可以再list.jsp中方便去获取
        req.setAttribute("list", list);
        //当前的Servlet中如何跳转到list.jsp页面
        req.getRequestDispatcher("/list.jsp").forward(req, resp);
    }

    /**
     * 删除留言
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置服务器端向客户端响应的内容的类型
        resp.setContentType("text/html;charset=utf-8");
        //设置客户端向服务器端发送请求的编码格式
        req.setCharacterEncoding("utf-8");
        int id = Integer.parseInt(req.getParameter("id"));
        GuestbookService service = new GuestbookServiceImpl();
        boolean isRigth = service.deleteBean(id);
        if (isRigth) {
            req.getRequestDispatcher("/manager?q=list").forward(req, resp);
        }

    }

    /**
     * 修改留言
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置服务器端向客户端响应的内容的类型
        resp.setContentType("text/html;charset=utf-8");
        //设置客户端向服务器端发送请求的编码格式
        req.setCharacterEncoding("utf-8");
    }

    /**
     * 从list.jsp跳转到 AddMessages.jsp
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void jump(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置服务器端向客户端响应的内容的类型
        resp.setContentType("text/html;charset=utf-8");
        //设置客户端向服务器端发送请求的编码格式
        req.setCharacterEncoding("utf-8");
        req.getRequestDispatcher("/AddMessages.jsp").forward(req, resp);
    }

    /**
     * 添加
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置服务器端向客户端响应的内容的类型
        resp.setContentType("text/html;charset=utf-8");
        //设置客户端向服务器端发送请求的编码格式
        req.setCharacterEncoding("utf-8");
        //要获取请求参数(前端页面的数据)
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        String email = req.getParameter("email");
        String title = req.getParameter("title");
        String content = req.getParameter("content");

        Date date = new Date();
        //如何对日期进行格式化SimpleDateFormat
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String createdTime = simpleDateFormat.format(date);

        //将请求参数保存到Guestbook对象中
        Guestbook gb = new Guestbook();
        gb.setName(name);
        gb.setPhone(phone);
        gb.setEmail(email);
        gb.setTitle(title);
        gb.setContent(content);
        gb.setCreatedtime(createdTime);

        //调用service中的添加的方法
        GuestbookService service = new GuestbookServiceImpl();
        boolean isRight = service.add(gb);
        if(isRight){
            req.getRequestDispatcher("/manager?q=list").forward(req, resp);
        }

    }


}



